#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 405;

int n, m;
ll c[N << 1];
ll x[N];
ll dp[N << 1][N << 1];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> c[i];
  rep(i, 1, n) cin >> x[i];
  rep(i, 1, n) c[i + n] = c[i];
  rep(i, 1, n * 2) dp[i][i] = x[c[i]] + 1;
  rep(len, 2, n) {
    rep(i, 1, n * 2 + 1 - len) {
      int j = i + len - 1;
      dp[i][j] = x[c[i]] + 1 + dp[i + 1][j];
      rep(k, i + 1, j) {
        if (c[k] == c[i]) {
          ll now = dp[k][j] + k - i + dp[i + 1][k - 1];
          dp[i][j] = min(dp[i][j], now);
        }
      }
    }
  }
  ll ans = 1e18;
  rep(i, 1, n - 1) ans = min(ans, dp[i][i + n - 1]);
  cout << ans;
  return 0;
}